<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h1>标题：商品管理系统</h1>
      <ul>
        <li v-for="(item, index) in products">
          商品名称：{{ item.name }}, 商品库存： {{ item.stock === 0 ? "无" :
          item.stock }}
          <button @click="changeStock(item, item.stock-1) ">-</button>
          <button @click="changeStock(item, item.stock+ 1)">+</button>
          <button @click="del(index)">删除</button>
        </li>
      </ul>
      <p>总库存：{{ totalStock }}</p>
    </div>

    <script src="./js/vue.js"></script>
    <script>
      const vm = new Vue({
        el: "#app",
        data: {
          products: [
            { name: "iPhone", stock: 10 },
            { name: "xiaomi", stock: 9 },
            { name: "huawei", stock: 8 },
          ],
        },
        methods: {
          changeStock(product, newStock) {
            if (newStock < 0) {
              newStock = 0;
            }
            product.stock = newStock;
          },
          del(index) {
            this.products.splice(index, 1);
          },
        },
        computed: {
          totalStock() {
            return this.products.reduce((pre, cur) => pre + cur.stock, 0);
          },
        },
      });
    </script>
  </body>
</html>
